package _06_动态规划;

public class _309_最佳买卖股票时机含冷冻期 {

    public static void main(String[] args) {

        _309_最佳买卖股票时机含冷冻期 v = new _309_最佳买卖股票时机含冷冻期();
        int[] ins = {1, 2, 3, 0, 2};
        System.out.println(v.maxProfit(ins));

    }

    // 定义三个状态，理清楚三个状态之间的关系即可
    public int maxProfit(int[] prices) {
        int len = prices.length;
        int[][] dp = new int[len][3];
        dp[0][2] = -prices[0];
        for (int i = 1; i < len; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i][1]);
            dp[i][1] = dp[i - 1][2] + prices[i];
            dp[i][2] = Math.max(dp[i - 1][0] - prices[i], dp[i][2]);
        }
        return Math.max(dp[len - 1][0], dp[len - 1][1]);
    }

}
